package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
/**
 * 
 * @author msjykf
 * 获取梦网营销、梦网直销、无限天利短信状态报告接口
 *
 */
public class SmsGetReportTask implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(SmsGetReportTask.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def timestamp = Util.getNowString("MMddHHmmss");
			PropertiesUtil pt= new PropertiesUtil();
			//梦网直销
			def userid = pt.readValue("mwzx_userid"); 
			def pwd = Util.getMD5Str(userid+pt.readValue("sms_str")+pt.readValue("mwzx_pwd")+timestamp); //userid + 固定字符串+ pwd(明文) + 时间戳，再进行MD5加密
			def smsip = pt.readValue("sms_ip");
			Map headMap=new HashMap(); //设置头文件
			headMap.put("Content-Type","application/json");
			JSONObject josnbody = new JSONObject();
			josnbody.put("userid",userid);
			josnbody.put("pwd",pwd);
			josnbody.put("timestamp",timestamp);
			josnbody.put("retsize",500);
			def url = "http://"+smsip+"/sms/v2/std/get_rpt";
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			def enableSSLflag = 0;
			//def msgjson = Util.doHttpPostUseHead(url,JSONObject.fromObject(josnbody).toString(),charset,timeout,enableSSLflag,headMap);
			def msgjson = """{"result":0,"rpts":[{"msgid":-5563739940248855315,"custid":"SM201811050001","pknum":1,"pktotal":1,"mobile":"15821991235","spno":"1069033301132020001","exno":"0001","stime":"2018-11-05 18:26:00","rtime":"2018-11-05 18:26:00","status":2,"errcode":"M2:0056","errdesc":"fail","exdata":"506044C6-247A-017A-8683-D6E220ADEA7F"},{"msgid":-5563796290219778842,"custid":"SM201811050001","pknum":1,"pktotal":1,"mobile":"13246476522","spno":"1069120501132020001","exno":"0001","stime":"2018-11-05 18:22:51","rtime":"2018-11-05 18:22:51","status":2,"errcode":"M2:0056","errdesc":"fail","exdata":"506044C6-247A-017A-8683-D6E220ADEA7F"}]}""";
			JSONObject obj = JSONObject.fromObject(msgjson);
			def i = 0;
			log.debug("result:"+obj.get("result"));
			if(obj.get("result")==0) {
				JSONArray rpts = obj.getJSONArray("rpts");
				for (i = 0; i < rpts.size(); i++) {
		            JSONObject it = rpts.get(i);
					log.debug("it:"+it);
					log.debug("mobile:"+it.get("mobile"));
					String[] datas=new String[3];
					if(it.get("status")==0) {
						datas[0] = "update sms_send_batch set prosperitysum=nvl(prosperitysum,0)+1 where batch_id='"+it.get("custid")+"'";
					}else {
						datas[0] = "update sms_send_batch set failuresum=nvl(failuresum,0)+1 where batch_id='"+it.get("custid")+"'";
					}
					datas[1] = "insert into sms_send(smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,createdate,expectedstartdate,createby,creategp,stime,trialcount,trialcountlimit,rstate,recode,rtime,exno,spno,pknum,pktotal,rdesc)select smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,sysdate,expectedstartdate,createby,creategp,stime,trialcount,trialcountlimit,"+it.get("status")+",'"+it.get("errcode")+"',to_date('"+it.get("rtime")+"','yyyy-mm-dd hh24:mi:ss'),exno,'"+it.get("spno")+"',"+it.get("pknum")+","+it.get("pktotal")+",'"+it.get("errdesc")+"' from sms_send_task where smsno="+it.get("exdata");
					datas[2] = "delete from sms_send_task where smsno="+it.get("exdata");
					log.debug("datas:"+datas);
					//int[] batchsql = eliteBean.updateBatch(datas,"dataSource");0	
				}
			}else {
				return msgjson
			}
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		}
	}
}
/**
public class SmsGetReportTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(SmsGetReportTask.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			def dateitem = format.format(map.get("dateitem")); //获取最后更新时间
			def now = format.format(new Date());
			def state = 1;
			//梦网直销短信网关数据
			def data=eliteBean.chaxun_sqlRowSet("select sid,status,SendTime,Param3 from tb_history where Param2 ='CALLCENTER' and SendTime>='"+dateitem+"' and SendTime<'"+now+"'","dataSourceMwzx");
			while(data.next()){
				String[] datas=new String[3];			
				if(data.getString(2).equals('DELIVRD')){
					state=1;
					datas[0] = "update sms_send_batch set prosperitysum=nvl(prosperitysum,0)+1 where batch_id='"+data.getString(4)+"'";
				}else{
					state=-1;
					datas[0] = "update sms_send_batch set failuresum=nvl(failuresum,0)+1 where batch_id='"+data.getString(4)+"'";
				};
				datas[1] = "insert into sms_send(smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,createdate,expectedstartdate,createby,creategp,senddate,trialcount,trialcountlimit,receivestate,receivecode,receivedate)select smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,sysdate,expectedstartdate,createby,creategp,senddate,trialcount,trialcountlimit,"+state+",'"+data.getString(2)+"',to_date('"+format.format(data.getDate(3))+"','yyyy-mm-dd hh24:mi:ss') from sms_send_task where smsno="+data.getInt(1);
				datas[2] = "delete from sms_send_task where smsno="+data.getInt(1);
				int[] batchsql = eliteBean.updateBatch(datas,"dataSource");	
			};
			//梦网营销短信网关数据
			data=eliteBean.chaxun_sqlRowSet("select sid,status,SendTime,Param3 from tb_history where Param2 ='CALLCENTER' and SendTime>='"+dateitem+"' and SendTime<'"+now+"'","dataSourceMwyx");
			while(data.next()){
				String[] datas=new String[3];			
				if(data.getString(2).equals('DELIVRD')){
					state=1;
					datas[0] = "update sms_send_batch set prosperitysum=nvl(prosperitysum,0)+1 where batch_id='"+data.getString(4)+"'";
				}else{
					state=-1;
					datas[0] = "update sms_send_batch set failuresum=nvl(failuresum,0)+1 where batch_id='"+data.getString(4)+"'";
				};
				datas[1] = "insert into sms_send(smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,createdate,expectedstartdate,createby,creategp,senddate,trialcount,trialcountlimit,receivestate,receivecode,receivedate)select smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,sysdate,expectedstartdate,createby,creategp,senddate,trialcount,trialcountlimit,"+state+",'"+data.getString(2)+"',to_date('"+format.format(data.getDate(3))+"','yyyy-mm-dd hh24:mi:ss') from sms_send_task where smsno="+data.getInt(1);
				datas[2] = "delete from sms_send_task where smsno="+data.getInt(1);
				int[] batchsql = eliteBean.updateBatch(datas,"dataSource");	
			};			
			
			def sqlresult = eliteBean.update("update sms_getreporttime set dateitem=to_date('"+now+"','yyyy-mm-dd hh24:mi:ss')","dataSource");
		}catch(Exception e) {
			log.error("错误代码： ",e);
		}
	}	
}
*/